// SrvrItem.cpp : implementation of the CAnsiEditorSrvrItem class
//

#include "stdafx.h"
#include "AnsiEditor.h"

#include "AnsiEditorDoc.h"
#include "SrvrItem.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAnsiEditorSrvrItem implementation

IMPLEMENT_DYNAMIC(CAnsiEditorSrvrItem, CDocObjectServerItem)

CAnsiEditorSrvrItem::CAnsiEditorSrvrItem(CAnsiEditorDoc* pContainerDoc)
	: CDocObjectServerItem(pContainerDoc, TRUE)
{
	// TODO: add one-time construction code here
	//  (eg, adding additional clipboard formats to the item's data source)
}

CAnsiEditorSrvrItem::~CAnsiEditorSrvrItem()
{
	// TODO: add cleanup code here
}

void CAnsiEditorSrvrItem::Serialize(CArchive& ar)
{
	// CAnsiEditorSrvrItem::Serialize will be called by the framework if
	//  the item is copied to the clipboard.  This can happen automatically
	//  through the OLE callback OnGetClipboardData.  A good default for
	//  the embedded item is simply to delegate to the document's Serialize
	//  function.  If you support links, then you will want to serialize
	//  just a portion of the document.

	if (!IsLinkedItem())
	{
		CAnsiEditorDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pDoc->Serialize(ar);
	}
}

BOOL CAnsiEditorSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize)
{
	// Most applications, like this one, only handle drawing the content
	//  aspect of the item.  If you wish to support other aspects, such
	//  as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this
	//  implementation of OnGetExtent should be modified to handle the
	//  additional aspect(s).

	if (dwDrawAspect != DVASPECT_CONTENT)
		return CDocObjectServerItem::OnGetExtent(dwDrawAspect, rSize);

	// CAnsiEditorSrvrItem::OnGetExtent is called to get the extent in
	//  HIMETRIC units of the entire item.  The default implementation
	//  here simply returns a hard-coded number of units.

	if(m_sizeExtent.cx + m_sizeExtent.cy)
	{
		rSize = m_sizeExtent;
		return TRUE;
	}
	CAnsiEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: replace this arbitrary size

	rSize = CSize(10000, 5000);   // 3000 x 3000 HIMETRIC units

	return TRUE;
}

BOOL CAnsiEditorSrvrItem::OnDraw(CDC* pDC, CSize& rSize)
{
	// Remove this if you use rSize
	UNREFERENCED_PARAMETER(rSize);

	CAnsiEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if(GetAppContext()->m_pDoc && GetAppContext()->m_pDoc->GetActivePage())
	{
		CSize sizeExtent;
		OnGetExtent(DVASPECT_CONTENT, sizeExtent);
		CRect rcBounds(0, 0, sizeExtent.cx, sizeExtent.cy);
		pDC->LPtoDP(rcBounds);

		pDC->SetWindowOrg(0, 0) ;
		pDC->SetWindowExt( rcBounds.Size() ) ;

		GetAppContext()->m_pDoc->GetActivePage()->Draw(pDC, rcBounds, FALSE);
	}

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAnsiEditorSrvrItem diagnostics

#ifdef _DEBUG
void CAnsiEditorSrvrItem::AssertValid() const
{
	CDocObjectServerItem::AssertValid();
}

void CAnsiEditorSrvrItem::Dump(CDumpContext& dc) const
{
	CDocObjectServerItem::Dump(dc);
}
#endif

/////////////////////////////////////////////////////////////////////////////

void CAnsiEditorSrvrItem::OnShow()
{
	gAppContext_IP = gAppContext;
	gpAppContext = &gAppContext_IP;
	CDocObjectServerItem::OnShow();
	//GetAppContext()->m_pDoc->m_bShowingEmbbed = TRUE;
}

void CAnsiEditorSrvrItem::OnOpen()
{
	CDocObjectServerItem::OnOpen();
}

void CAnsiEditorSrvrItem::OnHide()
{
	gpAppContext = &gAppContext;
	GetAppContext()->m_pDoc->CheckNotifyChanged();
	GetAppContext()->m_pDoc->m_bShowingEmbbed = FALSE;
	CDocObjectServerItem::OnHide();
}

BOOL CAnsiEditorSrvrItem::OnSetExtent(DVASPECT nDrawAspect, const CSize& size)
{
	BOOL bOk = CDocObjectServerItem::OnSetExtent(nDrawAspect, size);
	if(nDrawAspect == DVASPECT_CONTENT)
	{
		GetDocument()->UpdateAllItems(NULL);
	}
	return bOk;
}